package com.cosina.game.crystallight.ui.battle.drawable;

import android.graphics.Canvas;
import android.graphics.Rect;

import com.cosina.game.crystallight.data.CrystalInfo;
import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.engine.util.CycleCount;
import com.cosina.game.crystallight.model.crystal.Box;
import com.cosina.game.crystallight.model.crystal.Crystal;
import com.cosina.game.crystallight.model.crystal.CrystalContainer;
import com.cosina.game.crystallight.model.crystal.CrystalSeed;
import com.cosina.game.crystallight.model.crystal.Desk;
import com.cosina.game.crystallight.model.crystal.Palm;
import com.cosina.game.crystallight.model.crystal.Tower;
import com.cosina.game.crystallight.ui.battle.BattleResource;
import com.cosina.game.crystallight.ui.battle.CrystalResouce;
import com.cosina.game.crystallight.ui.battle.drawadapter.BaseDrawable;
import com.cosina.game.crystallight.ui.common.PaintSuite;
import com.cosina.game.crystallight.util.CanvasUtil;

public class CrystalDrawable extends BaseDrawable {

	@Override
	public void draw(Canvas canvas) {
		drawCrystalInTower(canvas);
		drawCrystalInBox(canvas);
	}
	
	private static void drawCrystalInTower(Canvas canvas){
		for(Tower tower : BattleEngine.architect.getTowers()){
			if(tower.isEmpty() == false){
				Crystal crystal = (Crystal)tower.get();
				int locationX = 5 + 44 * tower.location.x + 8;
				int locationY = 5 + 44 * tower.location.y + 6;
				drawCrystal(canvas,crystal,locationX, locationY);
				drawTwobar(canvas,crystal,locationX, locationY);
				
				locationX = locationX + 19;
				locationY = locationY + 32;
				drawAttack(canvas,crystal,locationX, locationY);
			}
		}
	}
	
	private static void drawAttack(Canvas canvas, Crystal crystal,
			int locationX, int locationY) {
		int attack = crystal.weapon.attack;
		if(attack >= 100){
			canvas.drawBitmap(BattleResource.digital[attack/100],locationX -10 , locationY,
					PaintSuite.p4Simple);
			canvas.drawBitmap(BattleResource.digital[attack%100/10],locationX - 3 , locationY,
					PaintSuite.p4Simple);
			canvas.drawBitmap(BattleResource.digital[attack%10],locationX + 4 , locationY,
					PaintSuite.p4Simple);
		}else{
			canvas.drawBitmap(BattleResource.digital[attack/10],locationX- 7 , locationY,
					PaintSuite.p4Simple);
			canvas.drawBitmap(BattleResource.digital[attack%10],locationX , locationY,
					PaintSuite.p4Simple);
		}
	}
	
	private static Rect rectA = new Rect();
	private static Rect rectB = new Rect();
	private static void drawTwobar(Canvas canvas, Crystal crystal,
			int locationX, int locationY) {
		if(crystal.level == CrystalInfo.MIN_LEVEL){
			return;
		}
		// 1
		//0 1
		{
			canvas.drawBitmap(CrystalResouce.speedBar[crystal.level * 2 -2],locationX -5 , locationY -1,
					PaintSuite.p4Simple);
			int height = (int)(41 * (1f-crystal.weapon.speedPercent));
			rectA.set(0, 0, 15, height);
			rectB.set(locationX -5, locationY -1, locationX -5 + 15, locationY -1 + height);
			canvas.drawBitmap(CrystalResouce.speedBar[crystal.level * 2 -1],rectA , rectB,
					PaintSuite.p4Simple);
		}
		{
			canvas.drawBitmap(CrystalResouce.rangeBar[crystal.level * 2 -2],locationX + 44 - 17, locationY -1,
					PaintSuite.p4Simple);
			int height = (int)(41 * (1f-crystal.weapon.rangePercent));
			rectA.set(0, 0, 15, height);
			rectB.set(locationX + 44 - 17, locationY -1, locationX + 44 - 17 + 15, locationY -1 + height);
			canvas.drawBitmap(CrystalResouce.rangeBar[crystal.level * 2 -1],rectA , rectB,
					PaintSuite.p4Simple);
		}
	}

	private static void drawCrystalInBox(Canvas canvas) {
		Desk desk = BattleEngine.desk;
		for(int i=0; i<6; i++) {
			Box box = desk.boxes[i];
			if(null != box.seed){
				int alphaBack = PaintSuite.p4Simple.getAlpha();
				CrystalSeed seed = box.seed;
				PaintSuite.p4Simple.setAlpha(seed.getEnegy());
				
				canvas.drawBitmap(BattleResource.genCrystalBitmaps[seed.color], 1 + 44 * i + (i>= 3 ? 54 : 0), 5 + 44 * 9,
						PaintSuite.p4Simple);
				PaintSuite.p4Simple.setAlpha(alphaBack);
			}
			if(false == box.isEmpty()){
				Crystal crystal = box.get();
				drawCrystal(canvas,crystal,1 + 44 * i + (i>= 3 ? 54 : 0) + 4, 5 + 44 * 9 + 4);
			}
		}
		
		if(false == desk.cuvette.isEmpty()){
			Crystal crystal = (Crystal)desk.cuvette.get();
			int locationX = 1 + 44 * 3 + 5 + 4, locationY = 5 + 44 * 9 + 4;
			drawCrystal(canvas,crystal,locationX, locationY);
			locationX = locationX + 19;
			locationY = locationY + 31;
			drawAttack(canvas,crystal,locationX, locationY);
		}
	}
	
	
	private static void drawCrystal(Canvas canvas,Crystal crystal, int x, int y){
		CrystalContainer container = Palm.getContainer();
		if(null != container && container.get() == crystal){
			int alphaFactor = getAlpha();
			CanvasUtil.drawAlphaBitmap(canvas,CrystalResouce.highLightCrystal[crystal.level],x-4,y-4,PaintSuite.p4Simple,alphaFactor);
			CanvasUtil.drawAlphaBitmap(canvas,CrystalResouce.getBitmap(crystal),x,y,PaintSuite.p4Simple,255-alphaFactor);
		}else{
			canvas.drawBitmap(CrystalResouce.getBitmap(crystal), x, y, PaintSuite.p4Simple);
		}
	}
	
	
	
	private static CycleCount aplha = new CycleCount(255);
	public static int getAlpha(){
		aplha.doCycle(20);
		return aplha.value;
	}
}
